import re

import allure
from playwright.sync_api import Page

"""
用户页面
"""
class UserPage:

    def __init__(self, page: Page):
        # 页面对象
        self.page = page
        # 页面地址
        self.url = "/xxl-job-admin/user"
        # 新增用户按钮
        self.locator_create_user_btn = page.get_by_role("button", name="新增用户")
        # 账号输入框
        self.locator_username_input = page.locator("#addModal").get_by_placeholder("请输入账号")
        # 密码输入框
        self.locator_password_input = page.locator("#addModal").get_by_placeholder("请输入密码")
        # 新增页-普通用户角色单选框
        self.locator_add_modal_role_opt = page.locator("#addModal input[name=\"role\"]").first
        # 新增页-管理员角色单选框
        self.locator_add_modal_role_admin = page.locator("#addModal input[name=\"role\"]").nth(1)
        # 保存按钮
        self.locator_save_btn = page.locator("#addModal").get_by_text("保存")
        # 新增成功提示
        self.locator_create_success_tip = page.get_by_text("新增成功")

        # <select class="form-control" id="role">
        #    <option value="-1">全部</option>
        #    <option value="1">管理员</option>
        #    <option value="0">普通用户</option>
        # </select>
        # 查询页-"角色"查询条件下拉框
        self.locator_role_search_page = page.locator("#role")

        # 查询页-账号关键词输入框
        self.locator_username_keyword_search_page = page.locator("#username")

        # 查询页-每页xx条记录选择器
        # <div class="dataTables_length" id="user_list_length">
        # <label>每页
        # <select name="user_list_length" aria-controls="user_list" class="form-control input-sm">
        # <option value="10">10</option>
        # <option value="25">25</option>
        # <option value="50">50</option><
        # option value="100">100</option><
        # /select>
        # 条记录<
        # /label>
        # </div>
        self.locator_page_size_selector = page.locator('xpath=//*[@id="user_list_length"]/label/select')
        # 查询页-”搜索“按钮
        self.locator_search_btn = page.get_by_role("button", name="搜索")

        # 查询页-数据列表框
        self.locator_user_list_tbody = page.locator('xpath=//*[@id="user_list"]/tbody')

        # 删除用户确认按钮
        self.locator_delete_confirm_btn = page.get_by_text("确定", exact=True)
        # 删除成功提示
        self.locator_delete_success_tip = page.get_by_text("成功")


    def navigate(self):
        with allure.step(f"导航到用户页:{self.url}"):
            self.page.goto(self.url)

    def click_create_user_btn(self):
        with allure.step("点击新增用户按钮"):
            self.locator_create_user_btn.click()

    def fill_username(self, username):
        with allure.step(f"输入账号:{username}"):
            self.locator_username_input.fill(username)

    def fill_password(self, password):
        with allure.step(f"输入密码:{password}"):
            self.locator_password_input.fill(password)

    def select_opt_role_add_modal(self):
        with allure.step("选择普通用户角色"):
            self.locator_add_modal_role_opt.check()

    def select_admin_role_add_modal(self):
        with allure.step("选择管理员角色"):
            self.locator_add_modal_role_admin.check()

    def click_save_btn(self):
        with allure.step("点击保存按钮"):
            self.locator_save_btn.click()

    def fill_username_keyword_search_page(self, username):
        with allure.step(f"输入账号关键词:{username}"):
            self.locator_username_keyword_search_page.fill(username)

    def select_all_role_search_page(self):
        with allure.step("点击角色下拉框选择全部角色"):
            self.locator_role_search_page.select_option("-1")

    def select_admin_role_search_page(self):
        with allure.step("点击角色下拉框选择管理员角色"):
            self.locator_role_search_page.select_option("1")

    def select_opt_role_search_page(self):
        with allure.step("点击角色下拉框选择普通用户角色"):
            self.locator_role_search_page.select_option("0")

    def select_page_size_10_search_page(self):
        with allure.step("点击分页大小下拉框选择每页10条记录"):
            self.locator_page_size_selector.select_option("10")

    def select_page_size_25_search_page(self):
        with allure.step("点击分页大小下拉框选择每页25条记录"):
            self.locator_page_size_selector.select_option("25")

    def select_page_size_50_search_page(self):
        with allure.step("点击分页大小下拉框选择每页50条记录"):
            self.locator_page_size_selector.select_option("50")

    def select_page_size_100_search_page(self):
        with allure.step("点击分页大小下拉框选择每页100条记录"):
            self.locator_page_size_selector.select_option("100")

    def click_search_btn(self):
        with allure.step("点击搜索按钮"):
            self.locator_search_btn.click()

    def click_delete_user(self, username):
        with allure.step(f"点击用户:{username}行中的删除按钮"):
            self.locator_user_list_tbody.get_by_role("row", name=re.compile(f"{username}.*")).get_by_role("button", name="删除").click()

    def click_confirm_delete_btn(self):
        with allure.step("点击确认删除按钮"):
            self.locator_delete_confirm_btn.click()